package wiki.banco;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexao
{

  private Connection connection;
  private PreparedStatement pstmt;
  
  private ConnectionFactory factory;

  public Conexao()
  {
    try
    {
      factory = new ConnectionFactory();
      this.connection = factory.getConnectionMySQL();
    } catch (SQLException ex)
    {
      Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void fechar()
  {
    try
    {
      this.connection.close();
      this.pstmt.close();
    } catch (SQLException ex)
    {
      System.err.println(Conexao.class.getName() + " " + ex.toString());
    }
  }

  public void preparar(String query)
  {
    try
    {
      this.pstmt = this.connection.prepareStatement(query);
    } catch (SQLException ex)
    {
      Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public ResultSet executeQuery() throws SQLException
  {
    return (this.pstmt.executeQuery());
  }

  public boolean executeUpdate() throws SQLException
  {
    return (this.pstmt.executeUpdate() > 0 ? true : false);
  }

  /**
   * @return the pstmt
   */
  public PreparedStatement getPstmt()
  {
    return pstmt;
  }
}
